<?php

class parser {
	// Class constructor
	public function __construct($folder, $pokersite) {
		$this->db = database::getInstance();
		$this->re = new regex();
		$this->ty = new tourney();
		$this->pokersite = $pokersite;
		$this->folder = $folder;
	
		$regexes = $this->re->fetchRegexes($this->pokersite);
		
		if($fh = opendir($folder)) {
			while(false != ($file = readdir($fh))) {
				$this->handhistory[] = $file;
			}
		}
	
		// Read entire folder then each file
		foreach($this->handhistory as $hh) {
			if(!is_array("$folder/$hh")) { 
				$hh = file("$folder/$hh", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); 
			}
			foreach($hh as $nr => $line) {
				foreach($regexes as $id => $r) {
					if(preg_match($r, $line, $matches)) {
						$m = $this->re->getRegexMethod($id);
						// Point to the respective function
						$this->$m($matches);
					}
				}
			}
		}
	}	

	// New hand
	public function newHand($m) {
		// Create new tourney if it doesn't exists
		if(!$this->ty->tourneyExists($m[2])) {
			$this->ty->tourneyAdd($m);
		}
		if(count($m) == 10) {
			$date = $m[8] . " " . $m[9];
			$this->db->sqlResult("
				INSERT INTO hands (hand, tid, blinds, date)
				VALUES(\"$m[1]\",\"$m[2]\",\"$m[7]\",\"$date\")
			");
			$this->currentHand = $m[1];
		}
	}
	
	// Recognize player name
	// (Most likely deprecated..)
	public function isPlayer($m) {

	}
	
	// Summarize table
	public function tableInfo($m) {
		if(count($m) == 5) {
			$this->db->sqlResult("
				UPDATE hands 
				SET tablenr=\"$m[2]\"
				WHERE hand=\"$this->currentHand\"
			");

		}	
	}
	
	// Current blind level 
	public function blinds($m) {
		
	}
	
	// Get players in current hand
	public function seatmap($m) {
		$this->db->sqlResult("
			INSERT INTO seatmap (hand, seat, player, chips)
			VALUES(\"$this->currentHand\", \"$m[1]\", \"$m[2]\", \"$m[3]\")
		");
	}
	
	// Fetch holecards
	public function holeCards($m) {
		if(count($m) == 3) {
			$this->db->sqlResult("
				UPDATE hands 
				SET holecards=\"$m[2]\"
				WHERE hand=\"$this->currentHand\"
			");
		}	
	}
	
	// Fetch stack sizes 
	public function stackSize($m) {
		
	}
	
	// Player actions 
	public function playerAction($m) {
		$m[3] = preg_replace('/^\s+/', '', $m[3]);
	}
	
	// FLOP 
	public function flop($m) {
		if(count($m) == 2) {
			$this->db->sqlResult("
				UPDATE hands 
				SET flop=\"$m[1]\"
				WHERE hand=\"$this->currentHand\"
			");
		}
	}
	
	// TURN
	public function turn($m) {
		if(count($m) == 2) {
			$this->db->sqlResult("
				UPDATE hands 
				SET turn=\"$m[1]\"
				WHERE hand=\"$this->currentHand\"
			");
		}
	}
	
	// RIVER
	public function river($m) {
		if(count($m) == 2) {
			$this->db->sqlResult("
				UPDATE hands 
				SET river=\"$m[1]\"
				WHERE hand=\"$this->currentHand\"
			");
		}
	}

	// Showdown
	public function showdown($m) {
		$this->db->sqlResult("
			UPDATE seatmap 
			SET holecards=\"$m[2]\"
			WHERE hand=\"$this->currentHand\"
			AND player=\"$m[1]\"
		");
	}
	
	// Hand winner
	public function winner($m) {
		if(count($m) == 3) {
			$this->db->sqlResult("
				UPDATE hands 
				SET winner=\"$m[1]\", pot=\"$m[2]\"
				WHERE hand=\"$this->currentHand\"
			");
		}
	}

	// Get busto player(s)
	public function busto($m) {
		
	}
	
	// Totalt pot
	public function pot($m) {
		
	}
	
	// Fetch entire board 
	public function board($m) {
	
	}
	
	// Fetch uncalled bets 
	public function uncalledBet($m) {
	
	}

	public function lastHandID() {
		$row = $this->db->singleRow("
			SELECT id 
			FROM hands 
			ORDER BY id 
			DESC 
			LIMIT 1
		");
		return $row['id'];
	}
}

?>
